package com.ethink.platform.manager;

import com.ethink.framework.common.constant.CommonRespCode;
import com.ethink.framework.common.exception.BusinessException;
import com.ethink.order.api.model.LogisticsTraceOrderRkConfirmDetailExcelImportModel;
import com.ethink.warehouse.api.LogisticsTraceOrderFacade;
import com.ethink.warehouse.api.enums.InventoryTypeEnum;
import com.ethink.warehouse.api.enums.LogisticsTraceStepEnum;
import com.ethink.warehouse.api.enums.SubjectEnum;
import com.ethink.warehouse.api.info.LogisticsTraceOrderInfo;
import com.ethink.warehouse.api.response.LogisticsTraceOrderGetDetailResponse;
import com.ethink.warehouse.api.response.LogisticsTraceOrderListRkConfirmDetailResponse;
import org.apache.commons.collections4.CollectionUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import org.springframework.web.multipart.MultipartFile;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

/**
 *
 * @Author: lzy
 * @Date: Created in 2023/1/31 11:16
 * ModifiedBy:
 * Version:
 */
@Component
public class LogisticsTraceOrderManager {
    @Autowired
    private ExcelImportManager excelImportManager;

    @Autowired
    private LogisticsTraceOrderFacade logisticsTraceOrderFacade;

    public List<LogisticsTraceOrderListRkConfirmDetailResponse> excelImport(Long logisticsTraceOrderId, MultipartFile file) {
        LogisticsTraceOrderGetDetailResponse logisticsTraceOrderGetDetail = logisticsTraceOrderFacade.getLogisticsTraceOrderById(logisticsTraceOrderId);
        LogisticsTraceOrderInfo logisticsTraceOrderInfo = logisticsTraceOrderGetDetail.getLogisticsTraceOrderInfo();
        if (!logisticsTraceOrderInfo.getSourceOrderType().equals(SubjectEnum.T11.getCode())) {
            throw new BusinessException(CommonRespCode.SYSTEM_ERR, "目前只有采购类型的跟进单支持此功能！");
        }
        if (!logisticsTraceOrderInfo.getStepStatus().equals(LogisticsTraceStepEnum.H.getCode())) {
            throw new BusinessException(CommonRespCode.SYSTEM_ERR, "当前状态不能使用此功能！");
        }

        List<LogisticsTraceOrderListRkConfirmDetailResponse> rkConfirmDetailList = logisticsTraceOrderFacade.listLogisticsTraceOrderRkConfirmDetail(logisticsTraceOrderId);
        if (CollectionUtils.isEmpty(rkConfirmDetailList)) {
            throw new BusinessException(CommonRespCode.SYSTEM_ERR, "数据导入错误！");
        }

        List<LogisticsTraceOrderListRkConfirmDetailResponse> response = new ArrayList<>();
        List<LogisticsTraceOrderRkConfirmDetailExcelImportModel> excelImportModelList = excelImportManager.transferClass(file, LogisticsTraceOrderRkConfirmDetailExcelImportModel.class);
        for (LogisticsTraceOrderRkConfirmDetailExcelImportModel excelImportModel : excelImportModelList) {
            List<LogisticsTraceOrderListRkConfirmDetailResponse> rkConfirmDetailFilterList = rkConfirmDetailList
                    .stream()
                    .filter(item -> item.getApplyDetailId().equals(excelImportModel.getApplyDetailId()) && item.getMateSku().equals(excelImportModel.getMateSku()))
                    .collect(Collectors.toList());
            if (CollectionUtils.isEmpty(rkConfirmDetailFilterList)) {
                throw new BusinessException(CommonRespCode.SYSTEM_ERR, "导入数据中有无法匹配的数据！");
            }
            LogisticsTraceOrderListRkConfirmDetailResponse rkConfirmDetailResponse = new LogisticsTraceOrderListRkConfirmDetailResponse();
            BeanUtils.copyProperties(rkConfirmDetailFilterList.get(0), rkConfirmDetailResponse);
            if (excelImportModel.getInventoryTypeName().equals(InventoryTypeEnum.ZP.getName())) {
                rkConfirmDetailResponse.setInventoryType(InventoryTypeEnum.ZP.getIntValue());
            } else if (excelImportModel.getInventoryTypeName().equals(InventoryTypeEnum.CC.getName())) {
                rkConfirmDetailResponse.setInventoryType(InventoryTypeEnum.CC.getIntValue());
            } else {
                throw new BusinessException(CommonRespCode.SYSTEM_ERR, "导入数据中有无法识别的库存状态数据！");
            }
            rkConfirmDetailResponse.setActualInQty(excelImportModel.getActualInQty());
            rkConfirmDetailResponse.setProduceCode(excelImportModel.getProduceCode());
            rkConfirmDetailResponse.setProductDate(excelImportModel.getProductDate());
            rkConfirmDetailResponse.setExpireDate(excelImportModel.getExpireDate());
            rkConfirmDetailResponse.setCountryOfOrigin(excelImportModel.getCountryOfOrigin());
            rkConfirmDetailResponse.setRemark(excelImportModel.getRemark());
            response.add(rkConfirmDetailResponse);
        }

        return response;
    }
}
